package com.fjnldx.mes.controller.shiftManagement;

import com.fjnldx.mes.domain.domainReq.AddSchedulingShift;
import com.fjnldx.mes.domain.domainReq.Id;
import com.fjnldx.mes.domain.domainReq.IdList;
import com.fjnldx.mes.domain.domainReq.QuerySchedulingShift;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.SchedulingShiftSystemRes;
import com.fjnldx.mes.service.shiftManagement.ShiftManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@RestController
@RequestMapping("/shiftManagement")
@Api(value = "轮班管理", tags = "轮班管理")
public class ShiftManageController {

    @Resource
    private ShiftManageService shiftManageService;

    @PostMapping("/queryShiftSystem")
    @ApiOperation(value = "查询班制", notes = "Api")
    public Result queryShiftSystem() {
        return shiftManageService.queryShiftSystem();
    }

    @PostMapping("/deleteShiftSystem")
    @ApiOperation(value = "班制删除", notes = "Api")
    public Result deleteShiftSystem(@RequestBody Id data) {
        return shiftManageService.deleteShiftSystem(data);
    }

    @PostMapping("/updateShiftSystem")
    @ApiOperation(value = "班制更新", notes = "Api")
    public Result updateShiftSystem(@RequestBody SchedulingShiftSystemRes params) {
        return shiftManageService.updateShiftSystem(params);
    }

    @PostMapping("/addShiftSystem")
    @ApiOperation(value = "班制增加", notes = "Api")
    public Result addShiftSystem(@RequestBody SchedulingShiftSystemRes params) {
        return shiftManageService.addShiftSystem(params);
    }

    @PostMapping("/addShift")
    @ApiOperation(value = "轮班增加", notes = "Api")
    public Result addShift(@RequestBody AddSchedulingShift params) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String bh = "LB"+dateTimeFormatter.format(LocalDateTime.now());
        return shiftManageService.addShift(params,bh);
    }

    @PostMapping("/deleteShift")
    @ApiOperation(value = "轮班删除", notes = "Api")
    public Result deleteShift(@RequestBody IdList params) {
        return shiftManageService.deleteShift(params);
    }

    @PostMapping("/updateShift")
    @ApiOperation(value = "轮班更新", notes = "Api")
    public Result updateShift(@RequestBody AddSchedulingShift params) {
        return shiftManageService.updateShift(params);
    }

    @PostMapping("/queryShift")
    @ApiOperation(value = "轮班查询", notes = "Api")
    public Result queryShift(@Validated @RequestBody QuerySchedulingShift params, Errors errors) {
        if (errors.hasErrors()) {
            return Result.error(Objects.requireNonNull(errors.getFieldError()).getDefaultMessage());
        }
        return shiftManageService.queryShift(params);
    }

    @GetMapping("/queryShiftDetail")
    @ApiOperation(value = "轮班查询明细", notes = "Api")
    public Result queryShiftDetail(String bh) {
        return shiftManageService.queryShiftDetail(bh);
    }

}
